package servlet;

import entity.Field;
import entity.Passage;
import service.CommonService;
import service.FieldService;
import service.PassageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet( name = "FieldServlet")
public class FieldServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用到的services
        CommonService commonService = new CommonService();
        PassageService passageService = new PassageService();
        FieldService fieldService = new FieldService();

        //获取课程名称
        String courseName = commonService.getCourseName();
        request.setAttribute("courseName", courseName);


        //获取目标field
        String fieldId = request.getParameter("field");
        Field field = fieldService.getFieldById(fieldId);
        request.setAttribute("field", field);

        //获取底部信息
        Passage bottomPassage = passageService.getPassageByField("5");
        String bottom_content = (bottomPassage == null) ? "暂无" : bottomPassage.getContent();
        request.setAttribute("bottom_content", bottom_content);

        //如果目标field是单文章页面设置对应field文章，否则获取对应列表
        switch (fieldId) {
            case "11":case "12":case "13":case "14": case "15":case "17":case "21":case "31":
            case "51":
                Passage passage = passageService.getPassageByField(fieldId);
                String content;
                content =(passage == null) ? "暂无" :passage.getContent();
                request.setAttribute("content", content);
                break;
            case "16":case "22":case "32": case "41":case "42":case "52":
            case "61":
                //如果目标field不是列表展示则无page，如果未给出page则进入第一页
                String page = request.getParameter("page");
                request.setAttribute("page",(page == null) ? 1 : Integer.valueOf(page));
                //获取文章总数用于分页
                int sum = passageService.getNumberOfFieldPassages(fieldId,2);
                int num_every_page = 10;
                int last_page = (int) Math.ceil((double)sum / num_every_page);
                if(sum == 0)
                    last_page = 1;
                request.setAttribute("last_page", last_page);
                //根据field和页数获取对应页数的文章
                List<Passage> passages = passageService.getFieldPassages(fieldId, (page == null) ? 1 : Integer.valueOf(page));
                request.setAttribute("passages", passages);
        }

        switch(fieldId){
            case "11":case "12": case "13":case "14": case "15":case "16":
            case "17": request.getRequestDispatcher("course_introduce.jsp").forward(request, response);break;
            case "21":
            case "22": request.getRequestDispatcher("teaching_team.jsp").forward(request, response);break;
            case "31":
            case "32": request.getRequestDispatcher("course_construction.jsp").forward(request, response);break;
            case "41":
            case "42": request.getRequestDispatcher("course_resource.jsp").forward(request, response);break;
            case "51":
            case "52": request.getRequestDispatcher("course_research.jsp").forward(request, response);break;
            case "61": request.getRequestDispatcher("questions.jsp").forward(request, response);break;
        }
    }
}
